package com.bj58.groupbuy.action.service.impl;

import java.util.ArrayList;
import java.util.List;

import com.bj58.groupbuy.action.db.CommonDao;
import com.bj58.groupbuy.action.db.DBConfig;
import com.bj58.groupbuy.action.model.NewBelongCate;
import com.bj58.groupbuy.action.service.NewBelongCateService;

public class NewBelongCateServiceImpl implements NewBelongCateService {
	private CommonDao  cmDao = new CommonDao();
	
	
	@Override
	public List<NewBelongCate> getAllBusinessType(String showBusi, boolean  isBiz) throws Exception {
		String business_type = "business_type";
		String business_type_name = "business_type_name";
		if(isBiz){
			business_type = business_type + "_biz";
			business_type_name = business_type_name + "_biz";
		}
		
		StringBuffer sql = new StringBuffer();
		sql.append(" select  *  ");
		sql.append(" from data_dict.m_common_belong_cate ");
		sql.append(" where  "+business_type_name+" is not null ");
		
		if(!"".equals(showBusi) && null != showBusi){
			StringBuffer tempShowBusi = new StringBuffer();
			for(String temp : showBusi.split(",")){
				tempShowBusi.append("'" + temp + "',");
			}
			sql.append(" and "+business_type+" in(" + tempShowBusi.substring(0,tempShowBusi.lastIndexOf(",")) + ") " );
		}
		sql.append(" group by "+business_type_name);
		
		List<NewBelongCate> rsList = cmDao.getAllColsByVo(sql.toString(), NewBelongCate.class, DBConfig.db_131_data_bi.getIntType());
		
		return rsList;
		
	}

	@Override
	public List<NewBelongCate> getLevelOne(String businesTypeName, boolean  isBiz)
			throws Exception {
		String business_type = "business_type";
		if(isBiz){
			business_type = business_type + "_biz";
		}
		
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT *  ");
		sql.append(" FROM data_dict.m_common_belong_cate ");
		sql.append(" where "+business_type+"  ='"+businesTypeName+"'  and cate1_name is not null ");
		sql.append(" group by  cate1_name");
		System.out.println("SQL:"+sql);
		return cmDao.getAllColsByVo(sql.toString(), NewBelongCate.class, DBConfig.db_131_data_bi.getIntType());
	}

	@Override
	public List<NewBelongCate> getLevelTwo(String businesTypeName,
			String cateOne, boolean  isBiz) throws Exception {
		String business_type = "business_type";
		if(isBiz){
			business_type = business_type + "_biz";
		}
		
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT *  ");
		sql.append(" FROM data_dict.m_common_belong_cate ");
		sql.append(" where cate1_name = '" +cateOne+"'  and cate2_name is not null  ");
		if(!"".equals(businesTypeName)){
			sql.append(" and "+business_type+"  ='"+businesTypeName+"' ");
		}
		sql.append(" group by  cate2_name");
		System.out.println("SQL:"+sql);
		return cmDao.getAllColsByVo(sql.toString(), NewBelongCate.class, DBConfig.db_131_data_bi.getIntType());
	}

	@Override
	public List<NewBelongCate> getLevelThree(String businesTypeName,
			String cateOne, String cateTwo, boolean  isBiz) throws Exception {
		String business_type = "business_type";
		if(isBiz){
			business_type = business_type + "_biz";
		}
		
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT *  ");
		sql.append(" FROM data_dict.m_common_belong_cate ");
		sql.append(" where cate1_name = '" +cateOne+"' ");
		sql.append(" and cate2_name = '" +cateTwo+"'  and cate3_name is not null  ");
		if(!"".equals(businesTypeName)){
			sql.append(" and "+business_type+"  ='"+businesTypeName+"' ");
		}
		sql.append(" group by  cate3_name");
		
		return cmDao.getAllColsByVo(sql.toString(), NewBelongCate.class, DBConfig.db_131_data_bi.getIntType());
	}


	@Override
	public List<NewBelongCate> getAllLevelOne() throws Exception {
		StringBuffer sql = new StringBuffer();
		sql.append(" SELECT cate_name,cate1_name  ");
		sql.append(" FROM data_dict.m_common_belong_cate ");
		sql.append(" where cate1_name is not null ");
		sql.append(" group by cate1_name");
		
		List<String> cols = new ArrayList<String>();
		cols.add("cate_name");
		cols.add("cate1_name");
		
		return cmDao.getCommonStatList(sql.toString() ,cols, NewBelongCate.class, DBConfig.db_131_data_bi.getIntType());
	}

	@Override
	public List<NewBelongCate> getAll() throws Exception {
		return cmDao.getAllColsByVo("select * from data_dict.m_common_belong_cate ", NewBelongCate.class, DBConfig.db_131_data_bi.getIntType());
	}


}
